<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/Snoopy.class.php');
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom_utf8.php');
class GetProxyCommand extends CConsoleCommand  
{
    public function run($args) {

    	$youdailiUrl="http://www.youdaili.cn/Daili/guonei/";
    	//先获得每天发布的代理入口
    	$snoopy = new Snoopy;
    	if(!$snoopy->fetch($youdailiUrl)) { 
      		return;
		}

		$html = str_get_html($snoopy->results);
		if(!isset($html)){
    		return;
    	}

    	$linkList = array();

    	//将已经发布的url和时间加入数组
    	foreach ($html->find("ul[class=newslist_line] li") as $key => $value) {
    		$time=$value->find("span[class=articlelist_time]", 0)->plaintext;
    		$time=str_replace("</font>","",$time);
    		$time=trim($time);
    		array_push($linkList, array('url' => $value->find("a", 0)->href, 'time' => $time));
    	}

    	$html->clear();
		unset($html);

		foreach ($linkList as $key => $value) {
			printf("%s\n", $value["url"]);
			if($snoopy->fetch($value["url"])) {
				$htmlIndex = str_get_html($snoopy->results);
				if(!isset($htmlIndex)){
		    		continue;
		    	}

		    	//首页，获取该页的分页总数
		    	$pageCount=$htmlIndex->find("ul[class=pagelist] li a", 0)->plaintext;
		    	$pageCount=str_replace("共","",$pageCount);
		    	$pageCount=str_replace("页:","",$pageCount);
		    	$pageCount=intval($pageCount);
		    	$i=2;
		    	$arrPager=array($value["url"],);
		    	while($i<=$pageCount){
		    		array_push($arrPager, str_replace(".html", "", $value["url"])."_".$i.".html");
		    		$i=$i+1;
		    	}

		    	foreach ($arrPager as $keyPager => $valuePager) {
		    		printf("[url=%s]\n", $valuePager);
		    		if($snoopy->fetch($valuePager)) {
		    			$htmlPager=str_get_html($snoopy->results);
		    			if(!isset($htmlPager)){
				    		continue;
				    	}

				    	$p=$htmlPager->find("div[class=cont_font] p", 0);
				    	if(!isset($p)){
				    		$p=$htmlPager->find("div[class=cont_font] span", 0);

				    		if(!isset($p)){
					    		$htmlPager->clear();
								unset($htmlPager);
								continue;
							}
				    	}
				    	$content=$p->innertext;
				    	$arrContent=explode('<br />', $content);
				    	foreach ($arrContent as $arrContentkey => $arrContentvalue) {
				    		$tmpArr=explode('@', $arrContentvalue);
				    		$tmpArr2=explode(':', $tmpArr[0]);
				    		if(count($tmpArr2)==2){
					    		$ip=trim($tmpArr2[0]);
					    		$port=trim($tmpArr2[1]);
					    		$desc=trim($tmpArr[1]);
					    		printf("[ip=%s] [port=%s] [desc=%s]\n", $ip, $port, $desc);

					    		$proxyObj=Proxy::model()->find(
					    			array(
					    				'condition'=>'ipaddress=:ipaddress and port=:port',
					    				'params'=>array(
					    							':ipaddress'=>$ip,
					    							':port'=>$port,
					    							)
					    				)
					    			);
								if (!isset($proxyObj)){
									printf(date("Y-m-d H:i:s") . " end find proxy [ip=%s] [port=%s] [desc=%s], it's not set\n", $ip, $port, $desc);
									$proxyObj=new Proxy;
									$proxyObj->ipaddress=$ip;
									$proxyObj->port=$port;
									$proxyObj->note=$desc;
									$proxyObj->status=0;
									$proxyObj->releasetime=$value["time"];
									$proxyObj->writetime=date("Y-m-d H:i:s");
									$proxyObj->save();
									//printf(date("Y-m-d H:i:s") . " [期号：%s] [序号：%s] [时间：%s] [类型：%s] [主队：%s] [让球：%s] [客队：%s] [主进：%s] [客进：%s] [胜：%s] [平：%s] [负：%s]\n", $value, $gameIndex, $gameTime, $gameType, $homeTeam, $rateBall, $awayTeam, $homeGoal, $awayGoal, $euroOdds3, $euroOdds1, $euroOdds0);
								}
				    		}
				    	}

				    	$htmlPager->clear();
						unset($htmlPager);
		    		}
		    	}
		    	$htmlIndex->clear();
				unset($htmlIndex);
			}
			else{
				printf("snoopy can not fetch the url:%s\n", $value["url"]);
			}
		}
    }
}